<?php


namespace app\user\controller;


use think\Controller;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Request;
use think\Db;
class Index extends Controller
{

    //首页
        public function index(){

            //轮播图
            $alterphoto=db('alterphoto')->order('sort','esc')->limit(4)->select();


//            dump($a);
//            die;
            //找出所有的一级分类
//            $onecate=db('cate')->where(['pid'=>0])->select();

            //找出特色肉食
            $meet=db('cate')->where(['pid'=>2])->select();
            $ids=[];
            foreach($meet as $k=>$v){
                $ids[]=$v['id'];
            }
            $meetgoods=db('goods')->where(['cate_id'=>$ids,'onshelf'=>1])->where('reserve','>',0)->limit(8)->order('id desc')->select();
           //找出膨化食品
            $penghua=db('cate')->where(['pid'=>1])->select();
            $ids=[];
            foreach($penghua as $k=>$v){
                $ids[]=$v['id'];
            }
            $penghuagoods=db('goods')->where(['cate_id'=>$ids,'onshelf'=>1])->where('reserve','>',0)->limit(8)->order('id desc')->select();

            //找出干果
            $ganguo=db('cate')->where(['pid'=>11])->select();
            $ids=[];
            foreach($ganguo as $k=>$v){
                $ids[]=$v['id'];
            }
            $ganguogoods=db('goods')->where(['cate_id'=>$ids,'onshelf'=>1])->where('reserve','>',0)->limit(8)->order('id desc')->select();

            //显示两条最新的产品
            $news=db('goods')->where(['onshelf'=>1])->where('reserve','>',0)->limit(2)->order('id','desc')->select();

            //小购物车
            $dcart=$this->getcart();
            $this->assign(['meetgoods'=>$meetgoods,
                'penghua'=>$penghua,
                'ganguo'=>$ganguo,
                'meet'=>$meet,
                'penghuagoods'=>$penghuagoods,
                'ganguogoods'=>$ganguogoods,
                'twonews'=>$news,
                //'OneCate'=>$onecate
                'cartarr'=>$dcart['arr'],
                'sum_price'=>$dcart['sum_price'],
                'total_num'=>$dcart['total_num'],
                'alter'=>json_encode($alterphoto)

                ]);
            return view();
    }


    //得到导航栏
        public function getcategoods($id=1,$qujian=''){
                //小购物车
                 $dcart=$this->getcart();

                $cate=db('cate')->where(['id'=>$id])->find();
                if ($cate['pid']==0){
                    $OnecateName=$cate['cate_name'];
                    $OnecateId=$id;
                    $goodscate=db('cate')->where(['pid'=>$id])->select();
                    $ids=[];
                    foreach($goodscate as $k=>$v){
                        $ids[]=$v['id'];
                    }
                    if($qujian==''){
                        $goods=db('goods')->where(['cate_id'=>$ids,'onshelf'=>1])->where('reserve','>',0)->paginate(6);
                    }else{
                        $q=explode('-',$qujian);
                        $goods=db('goods')->where('sale_price',"<=" ,"$q[1]")->where('sale_price',">=","$q[0]")->where(['cate_id'=>$ids,'onshelf'=>1])->where('reserve','>',0)->paginate(6);

                    }


                }else{
                    $goods=db('goods')->where(['cate_id'=>$id,'onshelf'=>1])->where('reserve','>',0)->paginate(6);
                    $gpid=db('cate')->field(['id','cate_name'])->where(['id'=>$cate['pid']])->find();

                    $OnecateName=$gpid['cate_name'];
                    $OnecateId=$gpid['id'];
                    $goodscate=db('cate')->where(['pid'=>$gpid['id']])->select();
//                $ids=[];
//                foreach($goodscate as $k=>$v){
//                    $ids[]=$v['id'];
//                }

                    //$goods=db('goods')->where(['cate_id'=>$ids])->paginate(6);

                }

                $this->assign(['goodscate'=>$goodscate,'goods'=>$goods,'OneCateName'=>$OnecateName,'id'=>$OnecateId,'cartarr'=>$dcart['arr'],
                    'sum_price'=>$dcart['sum_price'],
                    'total_num'=>$dcart['total_num'],'qujian'=>$qujian]);
                //dump(Db::name('goods')->getLastSql());
                //die();
            return  view();
    }


    public function ajaxgetcategoods(){
        $data=input('get.');
        if(empty($data['qujian'])){
            $goods=db('goods')->where(['cate_id'=>$data['id'],'onshelf'=>1])->where('reserve','>',0)->paginate(6);
        }else{
            if($data['qujian']=='其他'){
                $data['qujian']='250-1000000';
            }
            $q=explode('-',$data['qujian']);
            $goods=db('goods')->where('sale_price',"<=" ,"$q[1]")->where('sale_price',">=","$q[0]")->where(['cate_id'=>$data['id'],'onshelf'=>1])->where('reserve','>',0)->paginate(6);
        }
        
        echo json_encode($goods);
    }

    //获取商品详情
        public function getgoodsdetail($id){

            $g=db('goods')->where('id',$id)->find();
            if($g['onshelf']==0){
                $this->error('商品已经下架了','/');
            }

            $user=Session::get('user');
            $collectgoodsid=Cache::smembers('user_collect_id_'.$user['id']);

            //判断是否收藏了
           if(in_array($id,$collectgoodsid)){
               $is_collect=1;
           }else{
               $is_collect=0;
           }
            //小购物车
            $dcart=$this->getcart();

           //评论
            $comment=db('comment')->alias('a')->field('a.*,b.name,b.picture')->join('user b',"a.goods_id=$id and a.is_common=1 and a.user_id=b.id")->paginate(6,false, ['query' => Request::instance()->param()]);

            $goods=db('goods')->field('a.*,b.*')->alias('a')->join('goodsattr b',"a.id=b.goods_id and a.id =$id and a.reserve>0 and a.onshelf=1")->find();
            $goodsphoto=db('photo')->where('goods_id',$id)->select();
            $goodshot=db('goods')->where(['onshelf'=>1])->where('reserve','>',0)->order('sale_num','desc')->limit(4)->select();
           // dump($goodsphoto);die;

            //评论


            $this->assign([
                'goods'=>$goods,
                'goodsphoto'=>$goodsphoto,
                'goodshot'=>$goodshot,
                'id'=>$id,
                'cartarr'=>$dcart['arr'],
                'sum_price'=>$dcart['sum_price'],
                'total_num'=>$dcart['total_num'],
                'jsonp'=>json_encode($goodsphoto),
                'is_collect'=>$is_collect,
                'comment'=>$comment
            ]);
            return view();
        }



        //我的订单
    public function myorder(){
        $user=Session::get('user');
            $orders=db('order')->where('user_id',$user['id'])->order('id','desc')->select();
            $this->assign('orders',$orders);
            return view();
    }


    //确认收货
    public function comfirm(){
        db('order')->where('id',input('post.id'))->update(['is_sure'=>1]);
        echo 1;
    }
//        public function iajax(){
//            dump(input('post.'));
//        }



    public function getcart(){

        $sum_price=0;
        $total_num=0;
        $user=Session::get('user');
        $data=Cache::hgetAll('user_id_'.$user['id']);
        foreach ($data as $k=>$v){                 //遍历键
            $v=json_decode($v);
            $a=db('goods')->field(['sale_price','thumb','goods_name'])->where('id',$k)->find();
            foreach ($v as $key=>$value){
                $arr[$k][$key]=$value;
                $arr[$k]['sale_price']=$a['sale_price'];
                $arr[$k]['thumb']=$a['thumb'];
                $arr[$k]['goods_name']=$a['goods_name'];
                $arr[$k]['id']=$k;
            }
            $arr[$k]['total_price']=$a['sale_price']*$arr[$k]['num'];
            $sum_price+=$arr[$k]['total_price'];
            $total_num=$total_num+$arr[$k]['num'];
        }

        if(empty($arr)){
            $arr=1;
        }
        return ['arr'=>$arr,'sum_price'=>$sum_price,'total_num'=>$total_num];

    }

        //前台评论列表
    public function comment(){
            $userorder=db('comment')->alias('a')->field(['a.id','b.thumb','b.goods_name'])->join('goods b','a.goods_id=b.id and a.is_common=0')->order('a.id','desc')->select();
            $this->assign('uorder',$userorder);
            return view();
    }

    //发表评论
    public function sendcomment($id){

        $user=Session::get('user');
            if(request()->isPost()){
                $data=input('post.');
                db('comment')->where(['id'=>$id,'goods_id'=>$data['goods_id']])->update(['content'=>$data['content'],'date'=>date('Y-m-d H:i:s'),'is_common'=>1]);
                $this->success('评论成功','index');
            }

            //找出是否存在评论
            $carr=db('comment')->where(['id'=>$id,'user_id'=>$user['id'],'is_common'=>0])->find();
            if(empty($carr)){
                $this->error('您未购买过此商品或已经评论此订单');
            }else{
                $goods=db('goods')->field(['id','goods_name'])->where('id',$carr['goods_id'])->find();
                $this->assign(['goods'=>$goods,'id'=>$id]);
            }
            return view();


    }

    //收藏商品
    public function collectajax(){
        $data=input('post.');
        $user=Session::get('user');
        if(!empty($user)){
//            $goods=Cache::get("user_collect_id_".$user['id']);
//            if (empty($goods)){
//                Cache::set("user_collect_id_".$user['id'],$data['id']);
//                echo 1;
//            }else{
//                Cache::del("user_collect_id_".$user['id']);
//                echo 2;
//            }
            $collectgoodsid=Cache::smembers('user_collect_id_'.$user['id']);
            if (in_array($data['id'],$collectgoodsid)){
                Cache::srem('user_collect_id_'.$user['id'],$data['id']);
                echo 2;
            }else{
                Cache::sadd("user_collect_id_".$user['id'],$data['id']);     //添加到集合中]
                echo 1;
            }

        }else{
            echo 0;
        }
        //Cache::sadd("user_collect_id_".$user['id'],$data['id']);

    }



    //搜索
    public function search($name){

            $where=[
                ['goods_name','like',"%$name%"],
            ];
            $search=db('goods')->where($where)->where(['onshelf'=>1])->where('reserve','>',0)->select();
            if (empty($search)){
                $search='';
            }
        $dcart=$this->getcart();
            $this->assign(['search'=>$search,'cartarr'=>$dcart['arr'],
                'sum_price'=>$dcart['sum_price'],
                'total_num'=>$dcart['total_num'],]);
            return view();
    }
}